import VueRouter from "vue-router";
import HomePage from "@/pages/Home/HomePage";
import SearchResultPage from "@/pages/Search/SearchResultPage";
import DocDetailPage from "@/pages/DocDetail/DocDetailPage";
import EditorPage from "@/pages/Editor/EditorPage";
import MyPage from "@/pages/MyPage/MyPage"
// import ManagementPage from "@/pages/Manage/ManagementPage";
import UploadPage from "@/pages/Upload/UploadPage";
import testVue from "@/pages/testVue";
import store from "../store";
import Vue from "vue";


Vue.use(VueRouter);

//添加以下代码
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = []


//创建一个路由器
const router = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/home",
      name: "home",
      meta: {
        hideMiniSearchBar: true,
      },
      component: HomePage,
    },
    {
      path: "/docdetail",
      name: "docdetail",
      meta: {
        hideMiniSearchBar: true,
      },
      component: DocDetailPage,
    },
    {
      path: "/testvue",
      name: "testvue",
      component: testVue,
    },
    {
      path: "/searchresult",
      name: "searchresult",
      meta: {
        hideMiniSearchBar: true,
      },
      component: SearchResultPage,
    },
    // {
    //   path: "/management",
    //   name: "management",
    //   component: ManagementPage,
    // },
    //系统管理
    {
      path: "/manage",
      name: "manage",
      component: () => import("@/pages/Manage/NewManagement.vue"),
      redirect: "/FileLog",
      children: [
        {
          path: "/FileLog",
          name: "FileLog",
          component: () => import("@/components/Management/File/FileLog.vue"),
        },
        {
          path: "/RegisterApproval",
          name: "RegisterApproval",
          component: () =>
            import("@/components/Management/User/RegisterApproval.vue"),
        },
        {
          path: "/ResetPasswordApproval",
          name: "ResetPasswordApproval",
          component: () =>
            import("@/components/Management/User/ResetPasswordApproval.vue"),
        },
        {
          path: "/UserList",
          name: "UserList",
          component: () => import("@/components/Management/User/UserList.vue"),
        },
      ],
    },
    //上传文档
    {
      path: "/upload",
      name: "upload",
      component: UploadPage,
    },
    {
      path: "/uploadResource",
      name: "uploadResource",
      component: () => import("@/pages/Upload/NewUpload.vue"),
    },
    {
      path: "/UploadInstall",
      name: "UploadInstall",
      component: () => import("@/pages/Upload/UploadInstall.vue"),
    },
    // 个人中心
    {
      path: "/pageMyself",
      name: "pageMyself",
      component: () => import("../pages/Myself/pageMyself.vue"),
      redirect: "/MyselfHome",
      children: [
        {
          path: "/MyselfHome",
          name: "MyselfHome",
          component: () => import("../components/Myself/MyselfHome.vue"),
        },
        {
          path: "/MyselfMessage",
          name: "MyselfMessage",
          component: () => import("../components/Myself/MyselfMessage.vue"),
        },
        {
          path: "/MyselfUpload",
          name: "MyselfUpload",
          component: () => import("../components/Myself/MyselfUpload.vue"),
        },
        {
          path: "/MyselfDownload",
          name: "MyselfDownload",
          component: () => import("../components/Myself/MyselfDownload.vue"),
        },
        {
          path: "/MyselfFavorite",
          name: "MyselfFavorite",
          component: () => import("../components/Myself/MyselfFavorite.vue"),
        },
        {
          path: "/MyselfData",
          name: "MyselfData",
          component: () => import("../components/Myself/MyselfData.vue"),
        },
      ],
    },
    // 登录注册
    {
      path: "/findPassword",
      name: "findPassword",
      meta: {
        hideNavMenu: true,
      },
      component: () => import("../pages/Login/findPassword.vue"),
    },
    {
      path: "/register",
      name: "register",
      meta: {
        hideNavMenu: true,
      },
      component: () => import("../pages/Login/register.vue"),
    },
    {
      path: "/",
      name: "login",
      meta: {
        hideNavMenu: true,
      },
      component: () => import("../pages/Login/login.vue"),
    },
    //文字编辑
    {
      path:"/editor",
      name:"editor",
      component:EditorPage,
    },
    //用户主页
    {
      path:"/myPage",
      name:"myPage",
      component:MyPage
    }
  ],
});
// 路由守卫
router.beforeEach((to, from, next) => {
  // to 到哪; from 从哪; next 跳转动作;
  if (!store.getters.userId) {
    // 未登录，跳转到登录界面
    if (to.path === "/") {
      //目标路由是login是跳出判断；
      next();
      return;
    }
    if (to.path === "/register" || to.path === "/findPassword") {
      next();
      return;
    }
    next("/");
  } else {
    if (to.path === "/manage") {
      if (!(store.getters.userType === 2 || store.getters.userType === 3)) {
        //非管理者目标路由是管理界面不跳转；
        return;
      } else {
        next();
      }
    }
    next();
  }
});
export default router;
